from time import clock as bclock
from McTree import *
from StoyGusfield import *
import os
import cPickle as pickle

def clock():
	'''Returns clock-thing in milliseconds.'''
	return bclock()*1000 

def mem(size="vsz"):
    """Generalization; memory sizes: rss, rsz, vsz."""
    return int(os.popen('ps -p %d -o %s | tail -1' %(os.getpid(), size)).read())

def writeToFile(filename, string, mode='a+'):
	'''Appends a newline character.'''
	f = open(filename, mode)
	string = string.replace('.',',')
	f.write(string+'\n')
	f.close()
	
fibostr = ('b','a')
fibono = 1
def getNewFibStr():
	'''
	Increases the Fibonacci string to the next counter
	and returns it.
	''' 
	global fibostr, fibono
	newstr = fibostr[0]+fibostr[1]
	fibostr = (fibostr[1], newstr)
	fibono += 1
	return newstr


out = 'time-build-and-search-onlybranching.csv'
#writeToFile(out, '"n";"build time [ms]";"search time [ms]";"result size"', 'w')

while fibono <= 25:
	getNewFibStr()

while fibono < 28:	#29th Fibonacci no.: 514229
	print 'Building over Fibonaccistring(%i), tree no.' %fibono,
	string = getNewFibStr()
	buildtime = float(0)
	searchtime = float(0)
	no = 10
	#Build tree no times
	for i in range(0,no):
		print i+1,
		start = clock()
		tree = McTree(string)
		buildtime += clock()-start
		start = clock()
		result = StoyGusfield(tree)
		searchtime += clock()-start
	totaltime = buildtime/no
	searchtime = searchtime/no
	write = '%(n)i;%(time).1f;%(search).1f;%(length)i' %{'n':len(string), 'time': totaltime, 'search': searchtime, 'length':(result[0]+result[1])}
	writeToFile(out, write)
	print
